//
//  SearchViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/18.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "SearchViewController.h"
#import "SelectView.h"
#import "SelectViewController.h"
#import "NetWorkManager.h"
#import "SearchDetailViewController.h"

@interface SearchViewController ()

@property (retain,nonatomic)NSMutableArray * dataArray;
@property (retain,nonatomic)UIScrollView * hotScrollView;
@property (retain,nonatomic)UISearchBar * search;
@property (retain,nonatomic)SearchDetailViewController * vc;

@end

@implementation SearchViewController

- (void)viewWillDisappear:(BOOL)animated{
    [_search setAlpha:0];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setHidden:NO];
    [self.tabBarController.tabBar setHidden:YES];
    [_search setAlpha:1];

}

- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = YES;
    _vc = [[SearchDetailViewController alloc] init];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    
    self.view.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    [super viewDidLoad];
    // 添加view
    [self createView];
    // 获取数据
    [self getData];
}

- (void)backAction{
        [self.navigationController popViewControllerAnimated:YES];
   
}
// 搜索按钮点击
- (void)searchAction{
    _vc = [[SearchDetailViewController alloc] init];
    _vc.searchKey = _search.text;
    [self addChildViewController:_vc];
    [self.view addSubview:_vc.view];
    [_search resignFirstResponder];
}

- (void)createView{
    _search = [[UISearchBar alloc] initWithFrame:CGRectMake(70, 0, theScreenWidth - 140, 40)];
    [self.navigationController.navigationBar addSubview:_search];
    SelectView * selectView = [[SelectView alloc] initWithFrame:CGRectMake(0, 190, theScreenWidth, 40)];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 20)];
    label.text = @"大家都在搜";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor grayColor];
    [self.view addSubview:label];
    selectView.backgroundColor = [UIColor whiteColor];
    selectView.lable.text = @"使用选礼神器快速挑选礼物";
    [self.view addSubview:selectView];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [selectView addGestureRecognizer:tap];
    
    
}

- (void)tapAction{
    SelectViewController * vc = [[SelectViewController alloc] init];
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)getData{
    __weak SearchViewController * weakself = self;
    NetWorkManager * manager = [[NetWorkManager alloc] init];
    [manager getSearchHotNameWithblock:^(id respond) {// 有数据的时候再添加UIScrollView
        _dataArray = [[NSMutableArray alloc] initWithArray:respond];
        int height = 0;
        if (_dataArray.count % 5 == 0) {
            height = (int)_dataArray.count / 4 * 30 + 10;// 
        }else
            height = (int)(_dataArray.count / 4 + 1) * 30 + 10;
        
        _hotScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, theScreenWidth, 130)];
        _hotScrollView.contentSize = CGSizeMake(theScreenWidth, height);
        _hotScrollView.backgroundColor = [UIColor whiteColor];
        [weakself.view addSubview:_hotScrollView];
        int locationX = 10;
        int locationY = 10;
        for (int i = 0; i < _dataArray.count; i ++) {
            NSString * string = [_dataArray objectAtIndex:i];
            if (locationX + string.length * 20 + 30 >= theScreenWidth) {
                locationX = 10;
                locationY += 40;
            }
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
            btn.frame = CGRectMake(locationX, locationY, string.length * 18 + 20, 30);
            btn.tag = 1001 + i;
            [btn addTarget:weakself action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            locationX += string.length * 18 + 30;
            [_hotScrollView addSubview:btn];
            UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
            lable.text = _dataArray[i];
            lable.textColor = [UIColor blackColor];
            lable.layer.borderWidth = 0.5;
            lable.layer.borderColor = [[UIColor grayColor]CGColor];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.font = [UIFont systemFontOfSize:13];
            lable.layer.cornerRadius = 5;
            lable.layer.masksToBounds = YES;
            [btn addSubview:lable];
        }
    }];
    
}

- (void)btnAction:(UIButton *)btn{
    _search.text = _dataArray[btn.tag - 1001];
    _vc = [[SearchDetailViewController alloc] init];
    _vc.searchKey = _search.text;
    [self addChildViewController:_vc];
    [self.view addSubview:_vc.view];
    [_search resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}



#pragma mark tableview协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 20;
    }else
        return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        if (_dataArray.count % 5 == 0) {
            return _dataArray.count / 4 * 30 + 10;
        }else
            return (_dataArray.count / 4 + 1) * 30 + 10;
        
    }else
        return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 80, 20)];
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = @"大家都在搜";
        lable.font = [UIFont systemFontOfSize:12];
        lable.textColor = [UIColor grayColor];
        return lable;
    }else
        return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell;
    if (indexPath.section == 0) {
        static NSString * cellId = @"cell1";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            int locationX = 10;
            int locationY = 10;
            cell = [[UITableViewCell alloc] init];
            for (int i = 0; i < _dataArray.count; i ++) {
                NSString * string = [_dataArray objectAtIndex:i];
                if (locationX + string.length * 20 + 30 >= theScreenWidth) {
                    locationX = 10;
                    locationY += 40;
                }
                UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(locationX, locationY, string.length * 18 + 20, 30)];
                btn.tag = 1001 + i;
                locationX += string.length * 18 + 30;
                [cell.contentView addSubview:btn];
                UIButton * button = (UIButton *)[cell.contentView viewWithTag:1001 + i];
                UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width, button.frame.size.height)];
                lable.text = _dataArray[i];
                lable.textColor = [UIColor blackColor];
                lable.layer.borderWidth = 0.5;
                lable.layer.borderColor = [[UIColor grayColor]CGColor];
                lable.textAlignment = NSTextAlignmentCenter;
                lable.font = [UIFont systemFontOfSize:13];
                lable.layer.cornerRadius = 5;
                lable.layer.masksToBounds = YES;
                [button addSubview:lable];
            }
        }
        
        
    }else{
        static NSString * cellId = @"cell2";
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] init];
            SelectView * view = [[SelectView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, 40)];
            view.lable.text = @"使用选礼神器快速挑选礼物";
            [cell.contentView addSubview:view];
        }
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
